package com.m3839.sdk.anti.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.m3839.sdk.anti.constant.AntiConstant;
import com.m3839.sdk.common.base.BaseBean;

import org.json.JSONObject;

/**
 * Description:FinalBean
 *
 * @author 陈宝阳
 * @create 2020/1/2 14: 38
 */
public class FinalBean extends BaseBean<FinalBean> implements Parcelable {

  public int time;
  public String func;
  public String url;
  public String target;

  public FinalBean(){}

  public FinalBean(JSONObject jsonObject) {
    parseBean(jsonObject);
  }

  public FinalBean(int time, String func) {
    this(time, func, "", "");
  }

  public FinalBean(int time, String func, String url, String target) {
    this.time = time;
    this.func = func;
    this.url = url;
    this.target = target;
  }

  protected FinalBean(Parcel in) {
    time = in.readInt();
    func = in.readString();
    url = in.readString();
    target = in.readString();
  }

  public static final Creator<FinalBean> CREATOR = new Creator<FinalBean>() {
    @Override
    public FinalBean createFromParcel(Parcel in) {
      return new FinalBean(in);
    }

    @Override
    public FinalBean[] newArray(int size) {
      return new FinalBean[size];
    }
  };

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(time);
    dest.writeString(func);
    dest.writeString(url);
    dest.writeString(target);
  }

  @Override
  public String toString() {
    return "FinalBean{" +
        "time=" + time +
        ", func='" + func + '\'' +
        ", url='" + url + '\'' +
        ", target='" + target + '\'' +
        '}';
  }

  @Override
  public FinalBean parseBean(JSONObject jsonObject) {
    if(jsonObject == null) {
      return null;
    }
    time = jsonObject.optInt(AntiConstant.KEY_TIME);
    func = jsonObject.optString(AntiConstant.KEY_FUNC);
    JSONObject urlJson = jsonObject.optJSONObject(AntiConstant.KEY_URL_INFO);
    if(urlJson != null) {
      url = urlJson.optString(AntiConstant.KEY_URL);
      target = urlJson.optString(AntiConstant.KEY_TARGET);
    }
    return this;
  }
}
